package com.youlai.system.service.lichuang126.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.converter.lichuang126.PredictedGradeConverter;
import com.youlai.system.mapper.lichuang126.PredictedGradeMapper;
import com.youlai.system.model.entity.lichuang126.PredictedGrade;
import com.youlai.system.model.form.lichuang126.PredictedGradeForm;
import com.youlai.system.model.query.lichuang126.PredictedGradeQuery;
import com.youlai.system.model.vo.lichuang126.PredictedGradeVO;
import com.youlai.system.service.lichuang126.PredictedGradeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PredictedGradeServiceImpl extends ServiceImpl<PredictedGradeMapper, PredictedGrade> implements PredictedGradeService {


    public final PredictedGradeConverter predictedGradeConverter;

    @Override
    public Page<PredictedGradeVO> getPredictedGradePage(PredictedGradeQuery queryParams) {

        //查询页数
        int pageNum = queryParams.getPageNum();
        //查询数量大小
        int pageSize = queryParams.getPageSize();
        //查询条件
        String keywords = queryParams.getKeywords();

        Page<PredictedGrade> predictedGradePage = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<PredictedGrade>()
                        /**
                         * .like 模糊查询，这里就是模糊查询学生姓名
                         * .select 就是将查询出来的信息显示出来，这里就是全显示
                         */
                        .like(StrUtil.isNotBlank(keywords), PredictedGrade::getStudentId, keywords)
                        .select(
                                PredictedGrade::getStudentId,
                                PredictedGrade::getStudentName,
                                PredictedGrade::getCourseId,
                                PredictedGrade::getCourseName,
                                PredictedGrade::getTimeGrade,
                                PredictedGrade::getTestGrade,
                                PredictedGrade::getPredictedId,
                                PredictedGrade::getPredictedGrade,
                                PredictedGrade::getExpectation
                        )
        );
        Page<PredictedGradeVO> predictedGradeVOPage = predictedGradeConverter.entityToPage(predictedGradePage);
        return predictedGradeVOPage;
    }

    @Override
    public PredictedGradeForm getPredictedGradeFrom(int studentId) {

        PredictedGrade predictedGrade = this.getOne(new LambdaQueryWrapper<PredictedGrade>()
                .eq(PredictedGrade::getStudentId, studentId)
                .select(
                        PredictedGrade::getStudentId,
                        PredictedGrade::getStudentName,
                        PredictedGrade::getCourseId,
                        PredictedGrade::getCourseName,
                        PredictedGrade::getTimeGrade,
                        PredictedGrade::getTestGrade,
                        PredictedGrade::getPredictedId,
                        PredictedGrade::getPredictedGrade,
                        PredictedGrade::getExpectation
                )
        );
        PredictedGradeForm predictedGradeForm = predictedGradeConverter.entityToForm(predictedGrade);

        return predictedGradeForm;
    }

    @Override
    public boolean savePredictedGrade(PredictedGradeForm predictedGradeForm) {
        PredictedGrade predictedGrade = predictedGradeConverter.formToEntity(predictedGradeForm);
        boolean result = this.save(predictedGrade);
        return result;
    }

    @Override
    public boolean updatePredictedGrade(int student_id,PredictedGradeForm predictedGradeForm) {
        PredictedGrade predictedGrade = predictedGradeConverter.formToEntity(predictedGradeForm);
        boolean result = this.updateById(predictedGrade);
        return result;
    }

    @Override
    public boolean deletePredictedGrade(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除数据为空");
        //
        List<Long> idsList = Arrays.asList(ids.split(","))
                .stream()
                .map(id -> Long.parseLong(id))
                .collect(Collectors.toList());

        // 删除字典数据项
        boolean result = this.removeByIds(idsList);
        return result;
    }
}



